//
//  AYSHomeSDViewController.m
//  AiYingShiProject
//
//  Created by lanou on 15/10/29.
//  Copyright (c) 2015年 李晓理. All rights reserved.
//

#import "AYSHomeSDViewController.h"
#import "AYSHomeSDTableViewCell.h"
#import "AYSRequestManager.h"
#import "AYSHomePageModel.h"
#import <UIImageView+WebCache.h>
#import "AYSCommonPlayViewController.h"

@interface AYSHomeSDViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property(nonatomic, strong)UITableView *mainTableView;
@property(nonatomic, strong)UIView *topView;
@property(nonatomic, strong)UIButton *leftButton;// 左侧按钮
@property(nonatomic, strong)NSMutableArray *searchDetialArr;
@end

@implementation AYSHomeSDViewController
#pragma wark ========================搜索详情数据请求
- (void)requestDataWithSearchDetail
{
    NSString *tableViewStr =  [_tableViewText   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *urlStr = [NSString stringWithFormat:@"http://doukantv.com/api/?cli=iphone&pageindex=1&ver=2.3.4&q=%@&sys_ver=9.1&pagesize=20&type=all", tableViewStr];
   [AYSRequestManager requestWithType:GET url:urlStr parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *resultArr = dic[@"result"];
        for (NSDictionary *resultDic in resultArr)
        {
            AYSHomePageModel *model = [[AYSHomePageModel alloc] init];
            [model setValuesForKeysWithDictionary:resultDic];
            [_searchDetialArr addObject:model];
            NSLog(@"SD model %@", model.programname);
        }
        [_mainTableView reloadData];
       if (_searchDetialArr.count == 0)
       {
           UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"抱歉，您搜索的影片暂时没有资源哦！" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
           [alterView show];
           
       }
       

    } error:^(NSError *error)
     {
         NSLog(@"电影网络请求失败");
     }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self topView];
    _searchDetialArr = [NSMutableArray arrayWithCapacity:0];
    [self requestDataWithSearchDetail];
    [self getMainTableView];
}

// 回到搜索界面
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma wark ========================创建tableView
- (void)getMainTableView
{
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = 120;
    [self.view addSubview:_mainTableView];
    // 注册
    [_mainTableView registerClass:[AYSHomeSDTableViewCell class] forCellReuseIdentifier:@"reuse"];
   }

#pragma wark ======================tableView的DataSource配置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _searchDetialArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSHomeSDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    AYSHomePageModel *model = _searchDetialArr[indexPath.row];
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:model.vthumburl]];
    cell.detailLabel.text = model.shortdesc;
    cell.titleLabel.text = model.programname;
    cell.roleLabel.text = model.craw;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AYSCommonPlayViewController *commonPlayVC = [[AYSCommonPlayViewController alloc] init];
    AYSHomePageModel *model = _searchDetialArr[indexPath.row];
    commonPlayVC.programeID = model.programID;
    [self.navigationController pushViewController:commonPlayVC animated:YES];
}



// 设置导航View上面的控件
- (UIView *)topView
{
    if (!_topView)
    {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
        _topView.backgroundColor = [UIColor blackColor];
        _leftButton = [UIButton buttonWithType: UIButtonTypeCustom];
        _leftButton.frame = CGRectMake(5, 30, 20, 20);
        [_leftButton setImage:[UIImage imageNamed:@"goback"] forState:(UIControlStateNormal)];
        [_leftButton addTarget:self action:@selector(getBackToHomeMainVCButton:) forControlEvents:UIControlEventTouchUpInside];
        _titleLable = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 10, 25, 100, 30)];
        _titleLable.textColor = [UIColor whiteColor];
        _titleLable.text = _tableViewText;
        
        [self.topView addSubview:_titleLable];
        [self.topView addSubview:_leftButton];
        [self.view addSubview:_topView];
    }
    return _topView;
}

- (void)getBackToHomeMainVCButton:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
